﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;
using CDStore3Tiers;


namespace CDStore3Tiers
{
    public class XLDangNhap
    {
        StoreCDDataContext db;
        public int quyenhan;

        public XLDangNhap()
        {
            db = new StoreCDDataContext();
        }
        public StoreCDDataContext getConnect()
        {
            return db;
        }
        public Table<TaiKhoan> getList()
        {
            return db.TaiKhoans;
        }
        public void addCasy(string user,string pass)
        {
            TaiKhoan u = new TaiKhoan();
            u.ID = user;
            u.Password = pass;

            db.TaiKhoans.InsertOnSubmit(u);
            db.SubmitChanges();
        }

        public bool login(string user, string pass)
        {
            if (string.IsNullOrEmpty(user)) throw new Exception("Nhập username !");
            if (string.IsNullOrEmpty(pass)) throw new Exception("Nhập password !");

            db = new StoreCDDataContext();

            if(db.TaiKhoans.Where(u=>u.ID.ToLower().Equals(user.ToLower()) && u.Password.Equals(pass.ToLower())).Count()>0)
                return true;
            else
                return false;
        }

        
        public string GetName(string id)
        {
            var pq = db.TaiKhoan_SelectByID(id).FirstOrDefault();
            string ten = pq.TenNV.ToString();
            return ten;
        }
        public int GetQuyenHan(string id)
        {
            var pq = db.TaiKhoan_SelectByID(id).FirstOrDefault();
            int qh = pq.QuyenHan;
            quyenhan = qh;
            return qh;
        }
        public string getPass(string id)
        {
            db = new StoreCDDataContext();
            var sPss = (from p in db.TaiKhoans
                        where p.ID == id
                        select p).FirstOrDefault();
            return sPss.Password.ToString();
        }
        //public bool ktMatKhau(string user, string pass)
        //{
        //    db = new StoreCDDataContext();
        //    if (db.PhanQuyens.Where(u => u.ID.ToLower().Equals(user.ToLower()) && u.Password.Equals(pass.ToLower())).Count() > 0)
        //        return true;
        //    else
        //        return false;    
        //}

        public void DoiMK(string id, string pass)
        {
            db = new StoreCDDataContext();
            TaiKhoan dn = db.TaiKhoans.Single(t => t.ID.Equals(id));
            dn.Password = pass;
            db.SubmitChanges();
        }
    }
}
